package kr.hyosang.dkbs.kiosk.server.news;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import kr.hyosang.dkbs.kiosk.server.Common;
import kr.hyosang.dkbs.kiosk.server.data.News;
import kr.hyosang.dkbs.kiosk.server.data.entity.NewsEntity;

import com.google.appengine.labs.repackaged.org.json.JSONArray;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.appengine.labs.repackaged.org.json.JSONObject;

@SuppressWarnings("serial")
public class GetList extends HttpServlet {
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		try {
			News news = new News();
			List<NewsEntity> list = news.getCurrentList();
			
			JSONObject data = new JSONObject();
			JSONArray itemList = new JSONArray();
			for(NewsEntity entity : list) {
				JSONObject item = new JSONObject();
				item.put("title", entity.title);
				item.put("content", entity.content);
				
				itemList.put(item);
			}
			
			data.put("newsList", itemList);
			
			resp.setContentType("application/json; charset=utf-8");
			resp.getOutputStream().write(Common.getJsonResponse(data).getBytes("UTF-8"));
		}catch(JSONException e) {
			e.printStackTrace();
		}
		
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

}
